﻿using System;

namespace Trackr.Core
{
    public static class TimeExtension
    {
        public static string ToReadableString(this TimeSpan span)
        {
            var formatted = String.Format("{0}{1}{2}{3}",
                                             span.Duration().Days > 0 ? String.Format("{0:0} days, ", span.Days) : String.Empty,
                                             span.Duration().Hours > 0 ? String.Format("{0:0} hours, ", span.Hours) : String.Empty,
                                             span.Duration().Minutes > 0 ? String.Format("{0:0} minutes, ", span.Minutes) : String.Empty,
                                             span.Duration().Seconds > 0 ? String.Format("{0:0} seconds", span.Seconds) : String.Empty);

            if (formatted.EndsWith(", ")) 
                formatted = formatted.Substring(0, formatted.Length - 2);

            if (String.IsNullOrEmpty(formatted)) 
                formatted = "0 seconds";

            return formatted;
        }
    }
}
